package com.spring.mvc.controller;

import com.spring.mvc.dao.DepartmentDao;
import com.spring.mvc.dao.EmployeeDao;
import com.spring.mvc.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.Filter;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by Admin on 9/23 0023.
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;


    @RequestMapping("/emps")
    public String list(Map<String, Object> map) {
        map.put("employee", employeeDao.getAll());
        return "list";
    }

    /**
     * 添加
     * 获取页面用的是get 请求,首先页面上要显示的是部门信息;所以首先要添加 Department
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/addEmp", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }

    @RequestMapping(value = "/addEmp", method = RequestMethod.POST)
    public String save(@Valid Employee employee, BindingResult result,Map<String,Object> map) {
        System.out.println("save: "+employee);
        if(result.getErrorCount()>0){
            System.out.println("出错了!");
            for(FieldError error:result.getFieldErrors()){
                System.out.println(error.getField()+":::::"+error.getDefaultMessage());
            }
            //验证出现错误,转向定制的页面;
            map.put("departments", departmentDao.getDepartments());
            return "input";
        }
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/emps";
    }

    /**
     * 要进行修改的操作;
     * 1.进入到修改的页面,用的是get 请求,通过占位符 id ,首先同样是显示department
     * 2.根据 id 值,获取对应的要修改的对象,表单回显;用的是post 请求;
     * 3.修改用的注解 @ModelAttribute :
     * 首先 @ModelAttribute获取对象的值,然后将表单的值赋给该对象,最后提交;
     *
     * @param id
     * @param map
     * @return
     */
    //②
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)//get方法可有可无
    public String input(@PathVariable("id") Integer id, Map<String, Object> map) {
        map.put("employee", employeeDao.getEmployee(id));
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }

//    //③
//    @RequestMapping(value = "/emp")
//    public String update(Employee employee){
//        employeeDao.save(employee);
//        return "redirect:/emps";
//    }

    //①
    @ModelAttribute
    public void getEmployee(@RequestParam(value = "id", required = false) Integer id,
                            Map<String, Object> map) {
        if (id != null) {
            map.put("employee", employeeDao.getEmployee(id));
        }
    }

//    @InitBinder
//    public void bind(WebDataBinder binder){
//        binder.setDisallowedFields("lastName");
//    }

}
